/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac4.admn;

import edu.uoc.tdp.pac4.beans.ClientVehicle;
import javax.swing.JInternalFrame;
import edu.uoc.tdp.pac4.comun.GestorAdministrativaInterface;
import edu.uoc.tdp.pac4.comun.PAC4Exception;
import edu.uoc.tdp.pac4.comun.Utils;
import edu.uoc.tdp.pac4.i18n.TDSLanguageUtils;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Locale;
import javax.swing.JOptionPane;

/**
 *
 * @author mirica
 */
public class GestionCliente extends JInternalFrame {
    
    static public enum Accion {ALTA, MODIFICACION, BAJA};
    private Accion accion;            
    
    private GestorAdministrativaInterface conexion;
    private final String URL = "localhost";
    private final String JNDI_NAME ="Serv_Administrativa";
    private final int PORT = 1099;     
    
    private ClientVehicle clientVehicle;
     
    /**
     * Creates new form GestionCliente
     */
    public GestionCliente(GestorAdministrativaInterface conn) {
        if(conn!=null)
            conexion=conn;
        initComponents();
        
        conectarServerRMI();
        
        seleccionDeIdioma();
        inicializarControles();
        
        lblMensaje.setVisible(false);
        setInicializarPantalla(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        btnCancelar = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        btnComprobar = new javax.swing.JButton();
        lblTituloCliente = new javax.swing.JLabel();
        lblNifBuscar = new javax.swing.JLabel();
        txtNifBuscar = new javax.swing.JTextField();
        lblMensaje = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        txtNombre = new javax.swing.JTextField();
        txtApellidos = new javax.swing.JTextField();
        txtCP = new javax.swing.JTextField();
        lblCP = new javax.swing.JLabel();
        lblApellidos = new javax.swing.JLabel();
        lblNombre = new javax.swing.JLabel();
        lblNifCampo = new javax.swing.JLabel();
        lblNif = new javax.swing.JLabel();
        lblTituloDatosCliente = new javax.swing.JLabel();
        lblNumClienteT = new javax.swing.JLabel();
        lblNumCliente = new javax.swing.JLabel();
        lblDireccion = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        lblTituloDatosVehiculo = new javax.swing.JLabel();
        lblMarca = new javax.swing.JLabel();
        lblMatricula = new javax.swing.JLabel();
        txtMarca = new javax.swing.JTextField();
        txtMatricula = new javax.swing.JTextField();
        lblModelo = new javax.swing.JLabel();
        lblBastidor = new javax.swing.JLabel();
        txtModelo = new javax.swing.JTextField();
        txtBastidor = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        btnCancelar.setText("CANCELAR");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnAceptar.setText("ACEPTAR");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnComprobar.setText("COMPROBAR");
        btnComprobar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnComprobarActionPerformed(evt);
            }
        });

        lblTituloCliente.setFont(new java.awt.Font("Calibri", 3, 14)); // NOI18N
        lblTituloCliente.setForeground(new java.awt.Color(51, 51, 255));
        lblTituloCliente.setText("CLIENTE");

        lblNifBuscar.setText("NIF");

        lblMensaje.setText("[MENSAJE]");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTituloCliente)
                    .addComponent(lblMensaje)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblNifBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtNifBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnComprobar)))
                .addContainerGap(178, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 11, Short.MAX_VALUE)
                .addComponent(lblTituloCliente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNifBuscar)
                    .addComponent(txtNifBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnComprobar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblMensaje)
                .addGap(12, 12, 12))
        );

        lblCP.setText("CP");

        lblApellidos.setText("APELLIDOS");

        lblNombre.setText("NOMBRE");

        lblNifCampo.setText("NIF");

        lblNif.setText("--");

        lblTituloDatosCliente.setFont(new java.awt.Font("Calibri", 0, 14)); // NOI18N
        lblTituloDatosCliente.setForeground(new java.awt.Color(51, 51, 255));
        lblTituloDatosCliente.setText("DATOS CLIENTE");

        lblNumClienteT.setText("NUM_CLIENTE");

        lblNumCliente.setText("--");

        lblDireccion.setText("DIRECCION");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(lblTituloDatosCliente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblNumClienteT)
                        .addGap(56, 56, 56)
                        .addComponent(lblNumCliente))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNifCampo)
                            .addComponent(lblCP)
                            .addComponent(lblApellidos)
                            .addComponent(lblNombre)
                            .addComponent(lblDireccion))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblNif, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(0, 0, Short.MAX_VALUE))))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtCP, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                                    .addComponent(txtDireccion))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTituloDatosCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNumClienteT)
                    .addComponent(lblNumCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblNifCampo)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(lblNif)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNombre))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblApellidos))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDireccion)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(txtCP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblCP)))
                .addGap(14, 14, 14))
        );

        lblTituloDatosVehiculo.setFont(new java.awt.Font("Calibri", 0, 14)); // NOI18N
        lblTituloDatosVehiculo.setForeground(new java.awt.Color(51, 51, 255));
        lblTituloDatosVehiculo.setText("DATOS VEHICULO");

        lblMarca.setText("MARCA");

        lblMatricula.setText("MATRICULA");

        lblModelo.setText("MODELO");

        lblBastidor.setText("BASTIDOR");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(lblTituloDatosVehiculo)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblMatricula)
                            .addComponent(lblMarca))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtMarca, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                            .addComponent(txtMatricula))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblModelo)
                            .addComponent(lblBastidor))))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtModelo, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                    .addComponent(txtBastidor))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTituloDatosVehiculo, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtMarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblMarca))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblModelo)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtBastidor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblBastidor)
                    .addComponent(lblMatricula))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar))
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnComprobarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnComprobarActionPerformed
        //Validar el NIF y comprobar si existe o no en BBDD
        String nif = txtNifBuscar.getText();
        if (UtilsAdmin.validadoNIF(txtNifBuscar.getText()))
        {
            lblNif.setText(nif);
            try {
                clientVehicle = conexion.obtenerCliente(nif); 
                if(clientVehicle != null) {
                    accion = Accion.MODIFICACION;
                    UtilsAdmin.setMensaje(lblMensaje, TDSLanguageUtils.getMessage("Admin.Cliente.CLIENTE_EXISTE_MODIFICACION"), true, false);
                    setInicializarPantalla(true);
                    getCargarCliente(clientVehicle);
                } 
                else {
                    accion = Accion.ALTA;
                    UtilsAdmin.setMensaje(lblMensaje, TDSLanguageUtils.getMessage("Admin.Cliente.CLIENTE_NO_EXISTE_ALTA"), true, false);
                    setInicializarPantalla(true);
                }
            } catch (RemoteException ex) {
                Utils.shownError(TDSLanguageUtils.getMessage("Admin.ErrorRMI"));
            } catch (PAC4Exception ex) {
                Utils.shownError(TDSLanguageUtils.getMessage("Admin.Error"));
            }
        }
        else {
            UtilsAdmin.setMensaje(lblMensaje, TDSLanguageUtils.getMessage("Admin.Cliente.NIF_BUSCAR_INCORRECTO"), true, true);
            //lblError.setText(TDSLanguageUtils.getMessage("Cliente.NIF_BUSCAR_OBLIGATORIO"));
            setInicializarPantalla(false);
            clientVehicle = null;
            lblNif.setText("--");
            lblNumCliente.setText("--");
        }
    }//GEN-LAST:event_btnComprobarActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        if(validarDatosCliente()){
            try {
                cargarDatosClientVehiculo();
                if(accion == Accion.ALTA){
                    clientVehicle = conexion.insertarClienteVehiculo(clientVehicle);
                    JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Admin.Cliente.Cliente_insertado"), TDSLanguageUtils.getMessage("Admin.Aviso"), JOptionPane.INFORMATION_MESSAGE);
                    getCargarCliente(clientVehicle);
                }
                else if(accion == Accion.MODIFICACION){
                    conexion.actualizarClienteVehiculo(clientVehicle);
                    JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Admin.Cliente.Cliente_modificado"), TDSLanguageUtils.getMessage("Admin.Aviso"), JOptionPane.INFORMATION_MESSAGE);
                }
            } catch (RemoteException ex) {
                Utils.shownError(TDSLanguageUtils.getMessage("Admin.ErrorRMI"));
            } catch (PAC4Exception ex) {
                Utils.shownError(TDSLanguageUtils.getMessage("Admin.Error"));
            }   
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GestionCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GestionCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GestionCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GestionCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GestionCliente(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnComprobar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel lblApellidos;
    private javax.swing.JLabel lblBastidor;
    private javax.swing.JLabel lblCP;
    private javax.swing.JLabel lblDireccion;
    private javax.swing.JLabel lblMarca;
    private javax.swing.JLabel lblMatricula;
    private javax.swing.JLabel lblMensaje;
    private javax.swing.JLabel lblModelo;
    private javax.swing.JLabel lblNif;
    private javax.swing.JLabel lblNifBuscar;
    private javax.swing.JLabel lblNifCampo;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblNumCliente;
    private javax.swing.JLabel lblNumClienteT;
    private javax.swing.JLabel lblTituloCliente;
    private javax.swing.JLabel lblTituloDatosCliente;
    private javax.swing.JLabel lblTituloDatosVehiculo;
    private javax.swing.JTextField txtApellidos;
    private javax.swing.JTextField txtBastidor;
    private javax.swing.JTextField txtCP;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtMarca;
    private javax.swing.JTextField txtMatricula;
    private javax.swing.JTextField txtModelo;
    private javax.swing.JTextField txtNifBuscar;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables

   private void seleccionDeIdioma() {
        Locale localLocale = new Locale("es", "ES");
        TDSLanguageUtils.setDefaultLanguage("i18n/messages");
    }
   
    private void inicializarControles() {
        lblTituloCliente.setText(TDSLanguageUtils.getMessage("Admin.Cliente.TituloBusqueda"));
        lblNifBuscar.setText(TDSLanguageUtils.getMessage("Admin.Cliente.DNI"));
        btnComprobar.setText(TDSLanguageUtils.getMessage("Admin.BotonBuscar"));
        lblTituloDatosCliente.setText(TDSLanguageUtils.getMessage("Admin.Cliente.TituloDatosCliente"));
        lblNumClienteT.setText(TDSLanguageUtils.getMessage("Admin.Cliente.NumCliente"));
        lblNifCampo.setText(TDSLanguageUtils.getMessage("Admin.Cliente.DNI"));
        lblNombre.setText(TDSLanguageUtils.getMessage("Admin.Cliente.Nombre"));
        lblApellidos.setText(TDSLanguageUtils.getMessage("Admin.Cliente.Apellidos"));
        lblDireccion.setText(TDSLanguageUtils.getMessage("Admin.Cliente.Direccion"));
        lblCP.setText(TDSLanguageUtils.getMessage("Admin.Cliente.CP"));
        lblTituloDatosVehiculo.setText(TDSLanguageUtils.getMessage("Admin.Cliente.TituloDatosVehiculo"));
        lblMarca.setText(TDSLanguageUtils.getMessage("Admin.Cliente.Marca"));
        lblModelo.setText(TDSLanguageUtils.getMessage("Admin.Cliente.Modelo"));
        lblMatricula.setText(TDSLanguageUtils.getMessage("Admin.Cliente.Matricula"));
        lblBastidor.setText(TDSLanguageUtils.getMessage("Admin.Cliente.Bastidor"));
        btnAceptar.setText(TDSLanguageUtils.getMessage("Admin.Aceptar"));
        btnCancelar.setText(TDSLanguageUtils.getMessage("Admin.Cancelar"));
    }
    
    private void conectarServerRMI() {
        try {
            Registry registry = LocateRegistry.getRegistry(URL,PORT);
            this.conexion = (GestorAdministrativaInterface)registry.lookup(JNDI_NAME);
        } catch (NotBoundException e) {
            Utils.shownError(TDSLanguageUtils.getMessage("Admin.ErrorRMI"));
            System.exit(0);
        } catch (RemoteException e) {
            Utils.shownError(TDSLanguageUtils.getMessage("Admin.ErrorRMI"));
            System.exit(0);
        } catch (Exception e) {
            Utils.shownError(TDSLanguageUtils.getMessage("Aplication.ExcpGeneral"));
            System.exit(0);
        }    
    }   
    
    private void getCargarCliente(ClientVehicle clientVehicle) throws PAC4Exception {
        try {
            lblNumCliente.setText(Integer.toString(clientVehicle.getNumCliente()));
            txtNombre.setText(clientVehicle.getNom());
            txtApellidos.setText(clientVehicle.getCognoms());
            txtDireccion.setText(clientVehicle.getAdresa());
            txtCP.setText(Integer.toString(clientVehicle.getCodiPostal()));

            txtMarca.setText(clientVehicle.getMarca());
            txtModelo.setText(clientVehicle.getModel());
            txtMatricula.setText(clientVehicle.getMatricula());
            txtBastidor.setText(clientVehicle.getNum_chasis());
        } catch (Exception e) {
            Utils.shownError(TDSLanguageUtils.getMessage("Admin.Cliente.ERROR_CARGAR_CLIENTE"));
        }  
    }

    private void setInicializarPantalla(boolean isEditable) {
        //txtNif.setEnabled(isEditable);
        txtNombre.setEnabled(isEditable);
        txtApellidos.setEnabled(isEditable);
        txtDireccion.setEnabled(isEditable);
        txtCP.setEnabled(isEditable);
        txtMarca.setEnabled(isEditable);
        txtModelo.setEnabled(isEditable);
        txtMatricula.setEnabled(isEditable);
        txtBastidor.setEnabled(isEditable);
                
        //lblNif.setText("--");
        lblNumCliente.setText("--");
        txtNombre.setText("");
        txtApellidos.setText("");
        txtDireccion.setText("");
        txtCP.setText("");
        txtMarca.setText("");
        txtModelo.setText("");
        txtMatricula.setText("");
        txtBastidor.setText("");
    }

    
    private boolean validarDatosCliente() {
        //Todos los campos son obligatorios
        if(txtNombre.getText().isEmpty() || txtApellidos.getText().isEmpty() || txtDireccion.getText().isEmpty() ||
                txtCP.getText().isEmpty() || txtMarca.getText().isEmpty() || txtMatricula.getText().isEmpty() || txtModelo.getText().isEmpty() ||
                txtBastidor.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Admin.Cliente.CAMPOS_OBLIGATORIOS"), TDSLanguageUtils.getMessage("Admin.Aviso"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        //Validar los númericos CP
        try {
            int cp = Integer.parseInt(txtCP.getText());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Admin.Cliente.CAMPOS_INVALIDOS"), TDSLanguageUtils.getMessage("Admin.Aviso"), JOptionPane.ERROR_MESSAGE);
            return false; 
        }
        
        return true;
    }

    private void cargarDatosClientVehiculo() throws PAC4Exception {
        try {
            if(clientVehicle == null)
                clientVehicle = new ClientVehicle();

            clientVehicle.setNif(lblNif.getText());
            clientVehicle.setNom(txtNombre.getText());
            clientVehicle.setCognoms(txtApellidos.getText());
            clientVehicle.setAdresa(txtDireccion.getText());
            clientVehicle.setCodiPostal(Integer.parseInt(txtCP.getText()));

            clientVehicle.setMarca(txtMarca.getText());
            clientVehicle.setModel(txtModelo.getText());
            clientVehicle.setMatricula(txtMatricula.getText());
            clientVehicle.setNum_chasis(txtBastidor.getText());
        } catch (Exception e) {
            Utils.shownError(TDSLanguageUtils.getMessage("Admin.Cliente.ERROR_CARGAR_CLIENTE"));
        } 
    }
}